package business_opportunity

// 在建项目详情，与 project server 的区别在于，project 是聚合拿地，在建项目，开盘三个维度的数据，所以单独区分

import (
	"context"
	"encoding/json"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/oppty_status"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/project"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/job_center"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
)

type BuildingServer struct {
	buildingService    project.BuildingServiceIface
	opptyStatusService oppty_status.OpptyStatusServiceIface
	jobService         job_center.JobCenterServiceIface
}

func NewBuildingServer(
	buildingService project.BuildingServiceIface,
	opptyStatusService oppty_status.OpptyStatusServiceIface,
	jobService job_center.JobCenterServiceIface,
) pb.BuildingServiceServer {
	return &BuildingServer{
		buildingService:    buildingService,
		opptyStatusService: opptyStatusService,
		jobService:         jobService,
	}
}
func (s *BuildingServer) BuildingList(ctx context.Context, request *pb.BuildingListRequest) (*pb.BuildingListResponse, error) {
	response, err := s.buildingService.BuildingList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "BuildingList request:%s , error: %v", request, err)
		return nil, err
	}
	return response, nil
}

func (s *BuildingServer) ExportBuildingList(ctx context.Context, request *pb.ExportBuildingListRequest) (*pb.ExportBuildingListResponse, error) {
	listResp, err := s.buildingService.BuildingList(ctx, &pb.BuildingListRequest{
		PageSize:          1,
		Page:              1,
		Keyword:           request.Keyword,
		Area:              request.Area,
		CompanyArea:       request.CompanyArea,
		CompanyType:       request.CompanyType,
		IsPriceGtAvg:      request.IsPriceGtAvg,
		PrjTypeNum:        request.PrjTypeNum,
		PrjPropertyNum:    request.PrjPropertyNum,
		StartDateStart:    request.StartDateStart,
		StartDateEnd:      request.StartDateEnd,
		CompleteDateStart: request.CompleteDateStart,
		CompleteDateEnd:   request.CompleteDateEnd,
		CompanyLabel:      request.CompanyLabel,
	})

	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportSaleList SaleList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	if listResp.Total == 0 {
		return nil, errors.New("导出数据为空")
	}
	// 支持最多导出1万条数据
	if listResp.Total > 10000 {
		listResp.Total = 10000
	}

	requestBytes, _ := json.Marshal(request)

	jobId, err := s.jobService.NewJob(ctx, constants.JobMethodExport, constants.JobTypeBuildingExport, listResp.Total, string(requestBytes))
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportBuildingList NewJob request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	_ = s.jobService.StartJob(ctx, jobId)

	return &pb.ExportBuildingListResponse{JobId: jobId, IsSucceed: true}, nil

}

func (s *BuildingServer) GetBuildingDetailBasic(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailBasicResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailBasic(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBasic request:%s , error: %v", request.Id, err)
		return nil, err
	}

	//oppty_status字段
	opptyStatusRequest := &pb.GetOpptyStatusRequest{
		DataSource:   constants.OpptySourceBuilding,
		DataSourceId: request.Id,
		DetailPage:   true,
	}
	opptyStatus, err := s.opptyStatusService.GetOpptyStatus(ctx, opptyStatusRequest)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetOpptyStatus,err:%s", err.Error())
	}
	response.OpptyStatus = opptyStatus.StatusName

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailTargetNum(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailTargetNumResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailTargetNum(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailTargetNum request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailBidList(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailBidListResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailBidList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailBidList request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailParticipatingUnits(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailParticipatingUnitsResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailParticipatingUnits(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailParticipatingUnits request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailContractList(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailContractListResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailContractList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailContractList request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailWorkingDrawingAudit(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailWorkingDrawingAuditResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailWorkingDrawingAudit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailWorkingDrawingAudit request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailConstructionPermit(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailConstructionPermitResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailConstructionPermit(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailConstructionPermit request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}

func (s *BuildingServer) GetBuildingDetailCompletionFiling(ctx context.Context, request *pb.BuildingDetailRequest) (*pb.BuildingDetailCompletionFilingResponse, error) {
	if request.GetId() == "" {
		return nil, ykerrcode.TogRPCError(errcode.BusinessOpportunityIdNotFoundError, "缺少参数")
	}
	// 获取详情
	response, err := s.buildingService.GetBuildingDetailCompletionFiling(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetBuildingDetailCompletionFiling request:%s , error: %v", request.Id, err)
		return nil, err
	}

	return response, nil
}
